package com.upcoming.common;

import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;

import java.util.Map;

/**
 * 作者：tianjiayuan
 * 创建时间：2018-05-10 10:54
 * 类描述：
 * 修改人：
 * 修改时间：
 */
public class AuthTicket extends Token {

    public final static int TICKET_STORE_EXPIRES_SECOND = 60 * 10;// token有效期 10分钟
    public final static long TICKET_STORE_EXPIRES_MILLISECOND = (long) TICKET_STORE_EXPIRES_SECOND * 1000l;// token有效期 10分钟

    private String ticket = null;
    private String appId = null;
    private String scope = null;
    private String userId = null;

    private final static String ISSUER = "SKA_2.0";

    private AuthTicket() {
        this.setIssueTime(System.currentTimeMillis());
        this.setExpires(System.currentTimeMillis() + TICKET_STORE_EXPIRES_MILLISECOND);
        this.setSignature(getUniqueID());//TODO 暂时没有实现签名
    }

    public static AuthTicket issue(final String appId, final String scope, final String userId) {
        AuthTicket authTicket = new AuthTicket();
        authTicket.setIssuer(ISSUER);
        authTicket.setTicket(getUniqueID());
        authTicket.setAppId(appId);
        authTicket.setScope(scope);
        authTicket.setUserId(userId);
        return authTicket;
    }

    /**
     * 将Token转为base64编码格式
     *
     * @return
     */
    @Override
    public String toBase64Encode() {
        StringBuffer input = new StringBuffer();
        input.append("{");
        input.append("\"expire\"").append(":").append(getExpires()).append(",");
        input.append("\"issueTime\"").append(":").append(getIssueTime()).append(",");
        input.append("\"issuer\"").append(":\"").append(getIssuer()).append("\",");
        input.append("\"ticket\"").append(":\"").append(getTicket()).append("\",");
        input.append("\"signature\"").append(":\"").append(getSignature()).append("\",");
        input.append("\"appId\"").append(":\"").append(getAppId()).append("\",");
        input.append("\"scope\"").append(":\"").append(getScope()).append("\",");
        input.append("\"userId\"").append(":\"").append(getUserId()).append("\"");
        input.append("}");
        return new sun.misc.BASE64Encoder().encode(input.toString().getBytes()).replaceAll("(?:\r\n|\n\r|\n|\r)", "");
    }

    /**
     * 将accessToken的base64字符串转为AccessToken对象
     *
     * @param base64Str
     * @return
     */
    public static AuthTicket decodeBase64(String base64Str) {
        JSONObject jsonToken = JSONObject.fromObject(new String(new Base64().decode(base64Str)));
        Map<String, Object> map = (Map<String, Object>) jsonToken;

        AuthTicket accessToken = new AuthTicket();
        Long expire = (Long) map.get("expire");
        Long issueTime = (Long) map.get("issueTime");
        String issuer = String.valueOf(map.get("issuer"));
        String signature = String.valueOf(map.get("signature"));
        String appId = String.valueOf(map.get("appId"));
        String scope = String.valueOf(map.get("scope"));
        String userId = String.valueOf(map.get("userId"));
        String ticket = String.valueOf(map.get("ticket"));

        accessToken.setExpires(expire);
        accessToken.setIssuer(issuer);
        accessToken.setIssueTime(issueTime);
        accessToken.setSignature(signature);
        accessToken.setAppId(appId);
        accessToken.setScope(scope);
        accessToken.setUserId(userId);
        accessToken.setTicket(ticket);

        return accessToken;
    }


    public String getTicket() {
        return ticket;
    }

    public void setTicket(String ticket) {
        this.ticket = ticket;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }
}
